package com.openfeint.internal;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts.People;
import android.util.Log;

public class AddressBook {
	public static int _NAME  = 0x0001; //返回值是否返回name
	public static int _NUMBER  = 0x0010; //返回值是否返回number
	//public static int _PHOTO = 0x10000; 
	public static int WITH_NAME  = 0x0100;//没有name时这条记录是否返回
	public static int WITH_NUMBER  = 0x1000; //没有number时这条记录是否返回
	
	

	
	/**
	 * 封装好的接口
	 * 获得同时拥有名字和号码记录
	 * 返回名字和号码 String格式 name|tel,...
	 * 
	 * 
	 * */
	public static List<PeopleBean> getInf(Context c) {
		try {
            return getAddInf(c, _NAME|_NUMBER|WITH_NAME|WITH_NUMBER);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
	}
	/**
	 * 没有封装好的接口
	 * 通过组合setting参数决定逻辑
	 * 
	 * 
	 * */
	public static List<PeopleBean> getAddInf(Context c, int setting) throws Exception {
	List<PeopleBean> add = new ArrayList<PeopleBean>();
	ContentResolver cr=c.getContentResolver(); 
	Cursor cursor=cr.query(People.CONTENT_URI, null, null, null, null); 
	PeopleBean pb;
		while (cursor.moveToNext()) {

			pb = new PeopleBean();

			// 取得联系人名字
			int nameFieldColumnIndex = cursor
					.getColumnIndex(People.DISPLAY_NAME);
			String contact = cursor.getString(nameFieldColumnIndex);
			// 取得电话号码
			int numberFieldColumnIndex = cursor.getColumnIndex(People.NUMBER);
			String number = cursor.getString(numberFieldColumnIndex);

			if ((setting & WITH_NUMBER) > 0) {
				if (number == null)
					continue;
			}
			if ((setting & WITH_NAME) > 0) {
				if (contact == null)
					continue;
			}

			// 添加内容
			if ((setting & _NAME) > 0) {
				pb.setName(contact);
			}
			if ((setting & _NUMBER) > 0) {
				pb.setTel(number);
			}
			add.add(pb);
		}
		return add;

	}
}
